//
//  PpEditViewController.swift
//  Palmpilot
//
//  Created by 杨伟毅 on 2018/10/30.
//  Copyright © 2018年 杨伟毅. All rights reserved.
//

import UIKit

class PpEditViewController: PpBaseViewController {
    
    
    @IBOutlet weak var titleTextField: UITextField!
    
    @IBOutlet weak var contentTextView: UITextView!
    var  baseModel = PpBaseModel()
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.title = PpLanguageSwitchTool.getString(key: "Write a note")
        let  rightBar = UIBarButtonItem.init(barButtonSystemItem: UIBarButtonSystemItem.save, target: self, action: #selector(saveFunc))
        if baseModel.contentString.isEmpty == false {
            let  rightSecondBar = UIBarButtonItem.init(title: "分享", style: .plain, target: self, action: #selector(shareContent))
            self.navigationItem.rightBarButtonItems = [rightSecondBar,rightBar]
        }
        self.navigationItem.rightBarButtonItem = rightBar
    }
    @objc func shareContent() ->Void {
        let shareView = UIActivityViewController.init(activityItems: [PpLanguageSwitchTool.getString(key: "shareSecondTips"),baseModel.contentString], applicationActivities: nil)
        shareView.excludedActivityTypes = [.print,.copyToPasteboard,.assignToContact,.saveToCameraRoll,.addToReadingList,.airDrop]
        self.present(shareView, animated: true, completion: nil)
    }
    
   @objc  func saveFunc() -> Void {
    if titleTextField.text?.isEmpty == true {
        showErrorTitle(errorTitle: PpLanguageSwitchTool.getString(key: "The heading cannot be empty"))
        return
    }
    if contentTextView.text?.isEmpty == true {
     showErrorTitle(errorTitle: PpLanguageSwitchTool.getString(key: "The content cannot be empty"))
        return
    }
    let  model = PpBaseModel()
    model.titleString = titleTextField.text!
    model.contentString = contentTextView.text
    if baseModel.baseDate.isEmpty {
        let formatter = DateFormatter.init()
        formatter.dateFormat = "yyyy-MM-dd mm:dd:ss"
        model.baseDate = formatter.string(from: Date())
    }
    PpFmdbTools.shared.insterModel(model: model)
     self.navigationController?.popViewController(animated: true)
    }
    func showErrorTitle(errorTitle:String) -> Void {
        let alertView = UIAlertController.init(title: PpLanguageSwitchTool.getString(key: "alertTitle"), message: errorTitle, preferredStyle: .alert)
        let sureAction = UIAlertAction.init(title: PpLanguageSwitchTool.getString(key: "sure"), style: .cancel) { (action) in
        }
        alertView.addAction(sureAction)
        self.present(alertView, animated: true, completion: nil)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        if baseModel.contentString.isEmpty == false ,baseModel.baseDate.isEmpty == false,baseModel.titleString.isEmpty == false {
            titleTextField.text = baseModel.titleString
            contentTextView.text = baseModel.contentString
        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
